﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sudoku
{
    class CRME
    {
        public string[,] data;
        public int MAX_ROWS = 9;
        public int MAX_COLUMNS = 9;
        public CRME(string[,] data)
        {
            this.data = data;
        }

        public string[,] init()
        {
            for (int i = 0; i < this.MAX_ROWS; i++)
            {
                for (int j = 0; j < this.MAX_COLUMNS; j++)
                {
                    if ((this.data[i, j] == null) || (this.data[i, j] == String.Empty))
                    {
                        this.data[i, j] = "123456789";
                    }
                    this.scanColumn(i, j);
                    this.scanRow(i, j);
                    this.scanMinigrid(i, j);
                }
            }
            return this.data;
        }

        public void scanColumn(int i, int j)
        {
            for (int c = 0; c < this.MAX_COLUMNS; c++)
            {
                if (c != j)
                {
                    if (this.data[i, j].IndexOf(this.data[i, c]) != -1)
                    {
                        if (this.data[i, c] != "")
                        {
                            this.data[i, j] = this.data[i, j].Replace(this.data[i, c], "");
                        }
                    }
                }
            }
        }

        public void scanRow(int i, int j)
        {
            for (int r = 0; r < this.MAX_ROWS; r++)
            {
                if (r != i)
                {
                    if (this.data[i, j].IndexOf(this.data[r, j]) != -1)
                    {
                        if (this.data[r, j] != "")
                        {
                            this.data[i, j] = this.data[i, j].Replace(this.data[r, j], "");
                        }
                    }
                }
            }
        }

        public void scanMinigrid(int i, int j)
        {
            int[] index = this.getIndexMinigrid(i, j);
            int iIndex = index[0];
            int jIndex = index[1];

            for (int io = iIndex; io < iIndex + 3; io++)
            {
                for (int jo = jIndex; jo < jIndex + 3; jo++)
                {
                    if ((io != i) && (jo != j))
                    {
                        if (this.data[i, j].IndexOf(this.data[io, jo]) != -1)
                        {
                            if (this.data[io, jo] != "")
                            {
                                this.data[i, j] = this.data[i, j].Replace(this.data[io, jo], "");
                            }
                        }
                    }
                }
            }
        }

        public int[] getIndexMinigrid(int i, int j)
        {
            int[] index = new int[] { 0, 0 };
            index[0] = ((int)(i / 3)) * 3;
            index[1] = ((int)(j / 3)) * 3;
            return index;
        }
    }
}
